package array;

import lombok.Data;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

/**
*@description: 对数组的操作
*@author: kejl
*@create: 2020-07-04 15:14
*/
public class ArrayTest {

    /**
     * Arrays.sort对数组进行排序
     */
    @Test
    public void arraySort(){
        Student[] students = new   Student[10];
        students[0] = new Student(3,"k");
        students[1] = new Student(2,"l");
        students[2] = new Student(1,"m");
        Arrays.sort(students,new Comparator<Student>(){
            @Override
            public int compare(Student o1, Student o2) {
                if (o1==null || o2==null){
                    return 0;
                }
                if(o1.getAge()>o2.getAge()){
                    return 1;
                }else if(o1.getAge()==o2.getAge()){
                    return 0;
                }
                return -1;
            }
        });
        for (Student student:students){
            if (student==null){
                continue;
            }
            System.out.println(student.getName());
        }
    }



    @Data
    class Student{
        public Student(int age,String name){
            this.age = age;
            this.name = name;
        }
        private String name;
        private int age;
    }
}
